package com.mk.exnotice.config;

import com.mk.exnotice.aop.ExceptionNoticeAop;
import com.mk.exnotice.event.ExceptionNoticeListener;
import com.mk.exnotice.handler.ExceptionHandler;
import com.mk.exnotice.properties.ExceptionNoticeProperties;
import com.mk.exnotice.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *  @描述: 异常通知配置类
 *  @author: maming
 *  @date: 2021/6/2
 */
@Configuration
@AutoConfigureAfter({ ExceptionNoticeEmailSendingConfig.class })
@EnableConfigurationProperties({ExceptionNoticeProperties.class})
@ConditionalOnProperty(prefix = ExceptionNoticeProperties.PREFIX, value = "enabled", matchIfMissing = false)
public class ExceptionNoticeConfig{

    @Autowired
    private freemarker.template.Configuration configuration;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    private MailProperties mailProperties;

    @Autowired
    private MailService mailService;

    @Bean
    public ExceptionNoticeProperties properties() {
        return new ExceptionNoticeProperties();
    }

    @Bean
    public ExceptionHandler exceptionHandler(){
        return new ExceptionHandler(applicationEventPublisher, properties());
    }

    @Bean
    public ExceptionNoticeAop exceptionNoticeAop() {
        return new ExceptionNoticeAop(exceptionHandler());
    }

    @Bean
    public ExceptionNoticeListener exceptionNoticeListener(){
        return new ExceptionNoticeListener(mailService, mailProperties, properties());
    }
}
